<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 100px;
            height: 100px;
            background-color: black;
            position: absolute;
            top: 0px;
            left: 0px;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        var box = document.querySelector(".box")
        var dir = "top"
        var speed = 10
        document.onkeydown = function (e) {
            var key = e.key
            if (key == "ArrowRight") {
                dir = "left"
                speed = Math.abs(speed)
            } else if (key == "ArrowLeft") {
                dir = "left"
                speed = -Math.abs(speed)
            } else if (key == "ArrowDown") {
                dir = "top"
                speed = Math.abs(speed)
            } else if (key == "ArrowUp") {
                dir = "top"
                speed = -Math.abs(speed)
            }
            console.log(dir)
        }
        setInterval(function () {
            var dirnum = parseInt(getComputedStyle(box)[dir])
            console.log(dirnum);
            box.style[dir] = dirnum + speed + "px"
        }, 100);
    </script>
</body>

</html>